package com.qyhl.cloud.webtv.module_integral.wallet.exchangegold;

import android.text.TextUtils;

import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.IntegralUserInfoBean;
import com.qyhl.cloud.webtv.module_integral.app.IntegralApplication;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class ExchangeGoldModel implements ExchangeGoldContract.ExchangeGoldModel {
    private ExchangeGoldContract.ExchangeGoldPresenter mPresenter;
    private final String tag = "ExchangeGold";

    public ExchangeGoldModel(ExchangeGoldContract.ExchangeGoldPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void onDestroy() {
//        OkHttpUtils.getInstance().cancelTag(tag);
        mPresenter = null;
    }

    @Override
    public void getIntegralData() {
        if (NetUtil.isNetworkAvalible(IntegralApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
            JSONObject jsonObject = new JSONObject();
            String userName = CommonUtils.getInstance().getUserName();
            try {
                jsonObject.put("name", "userInfo");
                jsonObject.put("version", PathConfigConstant.version);
                jsonObject.put("username", userName);
                jsonObject.put("siteId", CommonUtils.getInstance().getSite());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            jsonObject.toString()
                    ))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<IntegralUserInfoBean>() {
                        @Override
                        public void onError(ApiException e) {
                            mPresenter.integralError(TextUtils.isEmpty(e.getMessage()) ? "请求失败" : e.getMessage());
                        }

                        @Override
                        public void onSuccess(IntegralUserInfoBean integralUserInfoBean) {
                            mPresenter.integralSuccess(integralUserInfoBean);
                        }
                    });
        } else {
            mPresenter.integralError("无网络连接，请检查您的网络...");
        }
    }

    @Override
    public void exchange(String text) {
        if (NetUtil.isNetworkAvalible(IntegralApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
            JSONObject jsonObject = new JSONObject();
            String userName = CommonUtils.getInstance().getUserName();
            try {
                jsonObject.put("name", "exchangeCoin");
                jsonObject.put("version", PathConfigConstant.version);
                jsonObject.put("username", userName);
                jsonObject.put("siteId", CommonUtils.getInstance().getSite());
                jsonObject.put("coin", text);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            jsonObject.toString()
                    ))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<String>() {
                        @Override
                        public void onError(ApiException e) {
                            if (e.getCode() == 22) {
                                mPresenter.exchangeError("兑换金币数量不足");
                            } else {
                                mPresenter.exchangeError(TextUtils.isEmpty(e.getMessage()) ? "请求失败" : e.getMessage());
                            }
                        }

                        @Override
                        public void onSuccess(String s) {
                            mPresenter.exchangeSuccess();
                        }
                    });

        } else {
            mPresenter.exchangeError("无网络连接，请检查您的网络...");
        }
    }
}
